package ru.ifmo.rybakov.muclumon.analysis2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ru.ifmo.rybakov.muclumon.Property;
import ru.ifmo.rybakov.muclumon.PropertyType;
import ru.ifmo.rybakov.muclumon.Task;

public class TaskCluster extends Cluster {
	private final List<Task> tasks = new ArrayList<Task>();
	
	public TaskCluster(Task task) {
		tasks.add(task);
	}
	
	public TaskCluster(Collection<Task> tasks) {
		this.tasks.addAll(tasks);
	}
	
	public Collection<Task> getTasks() {
		return Collections.unmodifiableCollection(tasks);
	}
	
	public Collection<?> getObjects() {
		return getTasks();
	}
	
	/* (non-Javadoc)
	 * @see ru.ifmo.rybakov.muclumon.analysis.IClusterManager#merge(ru.ifmo.rybakov.muclumon.analysis.ICluster, ru.ifmo.rybakov.muclumon.analysis.ICluster)
	 */
	@Override
	public Cluster merge(Cluster other) {
		assert (other instanceof TaskCluster) : "Invalid cluster type";
		TaskCluster otherTaskCluster = (TaskCluster) other;
		List<Task> tasks = new ArrayList<Task>();
		tasks.addAll(this.tasks);
		tasks.addAll(otherTaskCluster.getTasks());
		return new TaskCluster(tasks);
	}
	
	public Set<Property> getPropertySet() {
		Set<Property> properties = new HashSet<Property>();
		for (Task task : getTasks()) {
			properties.addAll(task.getPropertySet());
		}
		return properties;
	}
	
	public Set<Property> getPropertySet(PropertyType type) {
		Set<Property> properties = new HashSet<Property>();
		for (Task task : getTasks()) {
			Set<Property> taskProperties = task.getPropertySet();
			for (Property p : taskProperties) {
				if (type.equals(p.getType())) {
					properties.add(p);
				}
			}
		}
		return properties;
	}
	
	public static TaskCluster[] toClusters(Collection<Task> tasks) {
		TaskCluster[] clusters = new TaskCluster[tasks.size()];
		int i = 0;
		for (Task t : tasks) {
			clusters[i] = new TaskCluster(t);
			i++;
		}
		return clusters;
	}
}
